package com.example.management.controller;

import com.example.management.pojo.Inbound;
import com.example.management.pojo.Inventory;
import com.example.management.pojo.Outbound;
import com.example.management.service.InventoryService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class InventoryController {

    private final InventoryService inventoryService;

    @Autowired
    public InventoryController(InventoryService inventoryService) {
        this.inventoryService = inventoryService;
    }

    //入库表新增+库存表新增
    @PostMapping("/inbound")
    public String inbound(@RequestBody Inbound inbound) {
        // 调用 service 层的 inbound 方法新增库存
        inventoryService.inbound(inbound);

        return "Inventory added successfully.";
    }

    //全部库存查询

    @GetMapping("/all")
    public List<Inventory> findAllInventory() {
        return inventoryService.findAllInventory();
    }

    @GetMapping("/inventories")
    public ResponseEntity<List<Inventory>> getInventoryByName(@RequestParam String itemName) {
        List<Inventory> inventoryByName = inventoryService.getInventoryByName(itemName);
        return ResponseEntity.ok().body(inventoryByName);
    }


    //库存信息修改
    @PostMapping("/update")
    public String updateInventory(@RequestBody Inventory inventory) {
        inventoryService.updateInventory(inventory);
        return "Inventory updated successfully";
    }

    //删除库存信息
    @DeleteMapping("/delete")
    public String deleteInventory(@RequestBody Inventory inventory) {
        inventoryService.deleteInventory(inventory);
        return "Inventory deleted successfully";
    }

    //出库操作 库存数量减少+出库表新增
    @PostMapping("/outbound")
    public String outbound(@RequestBody Outbound outbound) {
        inventoryService.outbound(outbound);
        return "Outbound added successfully";
    }



}